package org.hive.managers.plugin
{
    import flash.text.StyleSheet;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.utils.Dictionary;
    
    import org.hive.Hive;
    import org.hive.managers.Manager;
    import org.hive.sequence.Sequence;

    public class TextManager extends Manager
    {
        public function TextManager(hive:Hive)
        {
            super(hive);
        }
        
        public static function init(hive:Hive):TextManager
        {
            if(Hive.DEBUG_MODE){trace( "StylesManager :: init" )}

            var manager:TextManager = new TextManager(hive);
            hive.registerManager(manager);
            
            //_____________ Register Object :: DisplayObject
            function textFieldFunc( action:*, sequence:Sequence, positionInSequence:int ):void {
                var tf:TextField = action as TextField;
                var params:Array = sequence.getParams(action);
                tf = manager.formatTextField(tf);
                if( params ){
                    var s:Object = params[0];
                    for( var p:String in s )
                    if( tf.hasOwnProperty(p) ) tf[p] = s[p];
                }
            }
            hive.sequenceManager.registerAction(TextField, textFieldFunc);
            return manager;
        }
        
        public function formatTextField( p_field:TextField ):TextField
        {
            p_field.styleSheet = getStyleSheet();
            p_field.autoSize = TextFieldAutoSize.LEFT;
            return p_field;
        }
        
        /**
         * 
         */        
        private const __styleSheetsByName:Dictionary = new Dictionary();
        
        /**
         * 
         */        
        private const __styleSheets:Dictionary = new Dictionary(true);
        
        /**
         * 
         * @param p_styleSheet
         * @param p_styleSheetName
         * 
         */        
        public function addStyleSheet( p_styleSheet:StyleSheet, p_styleSheetName:String='css' ):void
        {   
            if ( __styleSheets[ p_styleSheet ] ) 
            throw new ArgumentError( "this StyleSheet instance is already set with the name" + p_styleSheetName );
            
            if ( __styleSheetsByName[ p_styleSheetName ] ) 
            throw ArgumentError( "a StyleSheet instance is already set with the name " + p_styleSheetName );
            
            __styleSheets[ p_styleSheet ] = p_styleSheetName;
            __styleSheetsByName[ p_styleSheetName ] = p_styleSheet;
        }
        
        /**
         * 
         * @param p_styleSheet
         * @return 
         * 
         */        
        public function removeStyleSheet( p_styleSheet:StyleSheet ):Boolean
        {
            if( __styleSheets[ p_styleSheet ] ){
                delete __styleSheetsByName[ __styleSheets[ p_styleSheet ] ];
                delete __styleSheets[ p_styleSheet ];
                return true;   
            }
            else return false;
        }
        
        /**
         * 
         * @param p_styleSheetName
         * @return 
         * 
         */        
        public function removeStyleSheetByName( p_styleSheetName:String ):Boolean
        {
            return removeStyleSheet( getStyleSheet(p_styleSheetName) );
        }
        
        /**
         * 
         * @param p_styleSheetName
         * @return 
         * 
         */        
        public function getStyleSheet( p_styleSheetName:String='css' ):StyleSheet
        {
            return __styleSheetsByName[ p_styleSheetName ];
        }
        
    }
}